package com.shumei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shumei.dao.mapper.SonCategoryMapper;
import com.shumei.dao.pojo.ParentCategory;
import com.shumei.dao.pojo.SonCategory;
import com.shumei.service.SonCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SonCategoryServiceImpl implements SonCategoryService {
@Autowired
    SonCategoryMapper sonCategoryMapper;

    @Override
    public List<SonCategory> selectSonCategory() {
        List<SonCategory> sonCategories=sonCategoryMapper.selectList(null);

        return sonCategories;
    }

    @Override
    public void insertSonCategory(SonCategory sonCategory) {
        SonCategory sonCategory1=selectByname(sonCategory.getSonName());
        if (sonCategory1!=null) {
            System.out.println("子分类名称已存在");
        }
        else {

            int i = sonCategoryMapper.insert(sonCategory);
            if (i > 0) {
                System.out.println("子分类插入成功");

            } else {
                System.out.println("子分类插入失败");

            }
        }
    }

    @Override
    public void updateSonCategory(String before,String after,Integer pid) {
        SonCategory sonCategory1=selectByname(before);

        QueryWrapper<SonCategory> wrapper=new QueryWrapper<SonCategory>();
        wrapper.eq("son_id",sonCategory1.getSonId());
        SonCategory sonCategory=new SonCategory();
        sonCategory.setSonName(after);
        sonCategory.setUpdateTime(new Date());
        sonCategory.setPId(pid);
        if (sonCategoryMapper.update(sonCategory,wrapper)>0){
            System.out.println("修改成功");
        }
        else {
            System.out.println("修改失败");
        }
    }

    @Override
    public void deleteSon(int sonId) {
        int i = sonCategoryMapper.deleteById(sonId);
        if (i >0){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }

    private SonCategory selectByname(String sonname){
        QueryWrapper<SonCategory> wrapper=new QueryWrapper<SonCategory>();
        wrapper.eq("son_name",sonname);
        SonCategory sonCategory=sonCategoryMapper.selectOne(wrapper);

        return sonCategory;
    }
}
